<?php require_once FLASHPARTIAL; ?>

<?php if ($this->userland->logged_in() && $this->Membership_model->is_owner($bricabox->id) && $this->bricabox->show_panel()): ?>
<div id="new_panel">
	<h1>Hi, welcome to your new BricaBox, <?php echo username($this->userland->current_user()); ?>.</h1>
	<h3>Now that you've created a BricaBox, make it your own!<br />
		You haven't done the things below yet. As you do each one, it'll disappear from this list.</h3>
	<ul>
		<li class="<?php if ($this->bricabox->show_step('desc')) echo 'undone'; ?>"><a href="/admin/general/basic">Tell us about your BricaBox</a></li>
		<li class="<?php if ($this->bricabox->show_step('template')) echo 'undone'; ?>"><a href="/admin/entry_templates">Add an entry template</a></li>
		<li class="<?php if ($this->bricabox->show_step('entry')) echo 'undone'; ?>"><a href="#" onclick="return Entry.showNewPanel();">Add an entry</a></li>
		<li class="<?php if ($this->bricabox->show_step('homepage')) echo 'undone'; ?>"><a href="/admin/layout">Build your index layout</a></li>
		<li class="<?php if ($this->bricabox->show_step('content')) echo 'undone'; ?>"><a href="/admin/layout">Build your entry layout</a></li>
		<li class="<?php if ($this->bricabox->show_step('member')) echo 'undone'; ?>"><a href="/admin/members">Add a member</a></li>
	</ul>
</div>
<?php endif; ?>

<?php if ($bricabox->customize_with == 'html_css' && $bricabox->custom_header_html): ?>
	<div style="position:absolute;top:-1000em;left:-1000em;display:none;visibility:hidden;height:0;">
		<?php
		/**
		 * Tag replacement
		 */
		ob_start();
		render_tabs($nav_tabs);
		ob_end_clean();
		ob_start();
		echo $bricabox->custom_header_html;
		$_parsed_html = ob_get_contents();
		global $replace;
		foreach ($replace as $id => $klass)
		{
			$_parsed_html = preg_replace('/\{current_tab_' . $id . '\}/',
				$klass,
				$_parsed_html);
		}
		
		// Generate the proper membership tab/login tab link
		$login_link 	= '/sign_up_or_log_in';
		$login_label 	= 'Sign up or log in';
		if ($this->userland->logged_in()):
		
			if ($mem = $this->Membership_model->is_member_of($bricabox->id,
				$this->userland->current_user()->id)): // current user is a member
		
					$login_link  = "/member/" . user_url($this->userland->current_user());
					$login_label = "My profile";
		
			elseif ($this->Membership_model->user_applied($bricabox->id, $this->userland->current_user()->id)):
				$login_link  = '/members';
				$login_label = 'Membership pending';
			else:
				$login_link  = '/join_up';
				$login_label = 'Join ' . $bricabox->name;
			endif;
		
		else:
			$login_link = '/sign_up_or_log_in';
		endif;
		//-------------
		
		$_parsed_html = preg_replace('/\{login_link\}/', $login_link, $_parsed_html);
		$_parsed_html = preg_replace('/\{login_label\}/', $login_label, $_parsed_html);
		
		ob_end_clean();

		?>
	</div>
	<?php echo $_parsed_html; ?>
<?php else: ?>
<div id="bricabox_header">
	<h1><a href="/"><?php echo $bricabox->name; ?></a></h1>
	<h2><?php echo $bricabox->description; ?></h2>
	
	<div id="bricabox_tabs">
		<ul>
			<?php echo render_tabs($nav_tabs); ?>
			
			<?php if ($this->userland->logged_in()): ?>
			
			<?php if ($mem = $this->Membership_model->is_member_of($bricabox->id, $this->userland->current_user()->id)): // current user is a member ?>
			<?php if ($this->userland->current_user()->id != $bricabox->user_id): // don't show for owner ?>
			<li id="nav_remove_member" style=""><a onclick="return confirm('Are you sure you no longer want to be a member of this BricaBox?');" href="/leave" title="Leave this BricaBox (cancel your membership)"><span>X</span></a></li>
			<?php endif; ?>
			<li id="nav_member" class="<?php if ($this->userland->current_user()->login == @$mem_user->login) echo 'current'; ?>">
				<a href="/member/<?php echo user_url($this->userland->current_user()); ?>" title="<?php echo $this->Membership_model->get_role_for($mem->role_id)->name; ?>, My <?php echo $bricabox->name;?> profile"><span>My Profile</span></a></li>
			<?php elseif ($this->Membership_model->user_applied($bricabox->id, $this->userland->current_user()->id)): ?>
			<li id="nav_member"><a href="/members"><span>Membership pending&hellip;</span></a></li>
			<?php else: ?>
			<li id="nav_member"><a href="/join_up"><span>Join <?php echo $bricabox->name; ?></span></a></li>
			<?php endif; ?>
			
			<?php else: ?>
			<li id="nav_member"><a href="/sign_up_or_log_in"><span>Sign up or log in</span></a></li>
			<?php endif; ?>
			
	
			<li id="nav_search_field">
				<input type="text" size="25" id="search_field" name="q" value="<?php echo form_prep(url_safe_base64(@$parts['search'], 'decode')); ?>" class="<?php if (!isset($parts['search'])) echo 'grey'; ?>" onfocus="Bricabox.searchFocus();" onblur="Bricabox.searchBlur();" />
			</li>
		</ul>
	</div>
	<div class="bottom_bar"></div>
</div>

<script type="text/javascript" charset="utf-8">
	Bricabox.searchBlur();
	Bricabox.handleSearch(<?php echo $bricabox->id; ?>);
</script>

<?php endif; ?>



<?php if (!preg_match('/content\/\d+\/edit/', $_SERVER['QUERY_STRING'])): ?>
<div id="new_entry" style="display: none;">
	<div>
		<?php if (!$this->userland->logged_in()): ?>
			<a style="color: #0085cf !important; border-color: #0085cf !important;" href="/sign_up_or_log_in">Sign up or log in</a> to add new entries to <?php echo $bricabox->name; ?>!
		<?php else: ?>
			
		<?php if ($this->Bricabox_model->count_content_types($bricabox->id) == 0): ?>
		
		<?php if ($this->userland->logged_in() && $this->Membership_model->is_owner($bricabox->id)): ?>
			Before you can add entries, you need to <a href="/admin/entry_templates" style="background: #fff; color: #0085cf;">add your first entry template</a>.
		<?php else: ?>
			This BricaBox's owner hasn't added any entry templates yet!
		<?php endif; ?>
		
		<?php
		elseif ($this->Bricabox_model->count_content_types($bricabox->id) == 1):
			$only_type = $this->Bricabox_model->get_content_types($bricabox->id)->first_row();
			if ($this->userland->user_role() >= $only_type->create_level):
		?>
		
		<span class="step">Add a new <?php echo $only_type->name; ?> to <?php echo $bricabox->name; ?>.
			<?php if ($only_type->name == "Entry"): ?><a href="/admin/entry_templates">Add and edit entry templates</a><?php endif; ?></span>
			
		<div id="new_entry_fields">
		</div>
		<script type="text/javascript" charset="utf-8">
			Entry.loadFieldsForNewEntry(<?php echo $bricabox->id; ?>, <?php echo $only_type->id; ?>, true);
		</script>
		
		<?php
			else:
		?>
		Sorry, you're not allowed to add new entries to this BricaBox until you <a style="color: #0085cf !important; border-color: #0085cf !important;" href="/join_up">join up</a>.
		<?php
			endif;
		elseif ($this->Bricabox_model->count_content_types($bricabox->id) > 1):
		?>
			
		<span class="step">1.</span> I want to add a new
		<select name="content_type_id" id="content_type_id" onchange="return Entry.loadFieldsForNewEntry(<?php echo $bricabox->id; ?>, false, false);">
			<option value="-1"></option>
			<?php
			$added_something = false;
			$content_types   = $this->Bricabox_model->get_content_types($bricabox->id);
			if ($content_types):
				foreach ($content_types->result() as $type):
					if ($this->userland->user_role() >= $type->create_level): // who can add these?
						$added_something = true;
			?>
			<option value="<?php echo $type->id; ?>"><?php echo $type->name; ?></option>
			<?php
					endif;
				endforeach;
			endif;
			
			if (!$added_something)
			{
				echo '<option value="" selected="selected">Sorry, you can\'t add any entries!</option>';
			}
			?>
		</select>
		to <?php echo $bricabox->name; ?>.
		<div id="new_entry_fields">
		</div>
		
		<?php endif; ?>
		
		<?php endif; ?>
	</div>
	<a href="#" onclick="return Entry.hideNewPanel();" class="close" title="Close"><img src="/assets/images/close.png" alt="Close" /></a>
</div>
<?php endif; ?>

<?php if ($this->flashy->flash_exists('new_entry')): ?>
<script type="text/javascript" charset="utf-8">
	Entry.showNewPanel();
</script>
<?php endif; ?>